﻿using System;

namespace NMbsDuplex.TCPServerX.Net.AsyncPool
{
    using System.Threading.Tasks;
    using System.Net.Http;

    #region  异步工具箱 
    /// HTTP 异步工具箱 C# 5.0新特性  await 和 async 
    public static class AsyncTool
    {
        /// <summary>
        /// 发送 HTTP请求
        /// </summary>
        /// <param name="urlAddress"> Url地址 </param>
        /// <returns> IIS网页源码字符串 </returns>
        public static string CurlV1(string urlAddress)
        {
            Task.Run(async () =>
            {
                string result = string.Empty;
                using (HttpClient request = new HttpClient())
                {
                    result = await request.GetStringAsync(urlAddress);
                    return result;
                }
            });
            return string.Empty;
        }
        public static string CurlV2(string baseUrl)
        {
            var http_request = System.Net.WebRequest.Create(baseUrl);
            var stream = http_request.GetResponse().GetResponseStream();
            using(var sr = new System.IO.StreamReader(stream))
            {
                var s = sr.ReadToEnd();
                return s;
            }
        }
    }
    #endregion
}
